#!/usr/bin/env python
# -*- coding:utf-8 -*-
from django import forms
from django.shortcuts import render

from Tbug import models
from Tbug.forms.bootstrap import BootStrapForm


class WikiModelForm(BootStrapForm, forms.ModelForm):
    class Meta:
        model = models.Wiki
        fields = ['title', 'parent', 'content']
        # exclude = ['project', 'depth', 'create_datetime']

    def __init__(self, request, type, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # 找到想要的字段把他绑定显示的数据重置
        # 数据 = 去数据库中获取 当前项目所有的wiki标题
        total_data_list = [("", "请选择"),]

        # instance = kwargs.get('instance')
        # print(instance.id)
        print(type)

        if type == 'edit':
            wiki_object = kwargs.get('instance')
            data_list = models.Wiki.objects.filter(project=request.tracer.project).exclude(id=wiki_object.id).values_list('id', 'title')
            depth_list = models.Wiki.objects.filter(project=request.tracer.project).exclude(id=wiki_object.id).values_list('id', 'depth')
            
            # for id in depth_list:
            #     for data_id in data_list:
            #         if data_id.get('id')==id.get('id'):
            #             data = id.get('depth')+data_id.get('title')
            #             print(data)

            # print(data_list)
            # print(depth_list)
            total_data_list.extend(data_list)
        elif type == 'add':
            data_list = models.Wiki.objects.filter(project=request.tracer.project).values_list('id', 'title')
            total_data_list.extend(data_list)
            print('隔开你们')
            print(total_data_list)

        self.fields['parent'].choices = total_data_list
        
